<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button
                type="primary"
                icon="el-icon-plus"
                v-if="permission.stock_transfer_add"
                @click="handleAdd"
        >新增
        </el-button>
        <!--<el-button type="danger"-->
                   <!--icon="el-icon-delete"-->
                   <!--v-if="permission.stock_transfer_delete"-->
                   <!--@click="handleDelete">删 除-->
        <!--</el-button>-->
      </template>
      <template #transferDate-search="{ disabled, size }">
        <div style="display: flex;flex-direction: start;">
          <el-select v-model="query.transferDateRule" placeholder="比较" filterable clearable>
            <el-option label="" value=""></el-option>
            <el-option value=">" label="大于">大于</el-option>
            <el-option value=">=" label="大于等于">大于等于</el-option>
            <el-option value="=" label="等于">等于</el-option>
            <el-option value="!=" label="不等于">不等于</el-option>
            <el-option value="<=" label="小于等于">小于等于</el-option>
            <el-option value="<" label="小于">小于</el-option>
          </el-select>
          <el-date-picker
                  size="default"
                  v-model="query.transferDateSearch"
                  type="date"
                  :format="dateFormat"
                  :value-format="dateFormat"
                  :unlink-panels="true"
                  clearable
                  placeholder="调拨日期"
                  :picker-options="pickerOptions"
          >
          </el-date-picker>
        </div>
      </template>
      <template #menu="{ row, size, type }">
        <el-tooltip content="查看" placement="top">
          <el-button
                  icon="el-icon-view"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleView(row)"
                  v-if="permission.stock_transfer_view"
          >
          </el-button>
        </el-tooltip>
<!--        <el-tooltip content="修改" placement="top">-->
<!--          <el-button-->
<!--                  icon="el-icon-edit"-->
<!--                  link-->
<!--                  :size="size"-->
<!--                  :type="type"-->
<!--                  @click="() => handleEdit(row)"-->
<!--                  v-if="permission.stock_transfer_edit"-->
<!--          >-->
<!--          </el-button>-->
<!--        </el-tooltip>-->
        <el-tooltip content="删除" placement="top">
          <el-button
                  icon="el-icon-delete"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowDel(row)"
                  v-if="permission.stock_transfer_delete"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
    <stock-transfer-form ref="stockTransferForm" @success="refreshChange"></stock-transfer-form>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/anwise/stock/stock_transfer";
  import {userCompany} from "@/api/anwise/common";
  import StockTransferForm from "./components/stock_transfer_form.vue";
  import {mapGetters} from "vuex";

  export default {
    components: {
      StockTransferForm
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        dateFormat: 'YYYY-MM-DD',
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: false,
          searchMenuSpan: 6,
          border: true,
          index: true,
          selection: true,
          reserveSelection:true,
          labelWidth: 150,
          searchLabelWidth:100,
          indexWidth:100,
          menuWidth: 210,
          addBtn: false,
          editBtn: false,
          viewBtn: false,
          delBtn: false,
          stripe: true,
          searchIndex: 3,
          searchIcon: false,
          searchEnter:true,
          column: [
            {
              label: "调拨单号",
              prop: "idNo",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入调拨单号",
                trigger: "blur"
              }],
              width:120,
              overHidden: true,
              sortable: true,
            },
            {
              label: "机构名称",
              prop: "deptName",
              type: "input",
              width:120,
              overHidden: true,
              sortable: true,
            },
            {
              label: "调出仓库名称",
              prop: "transferOutName",
              type: "input",
              width:160,
              overHidden: true,
              sortable: true,
            },
            {
              label: "调入仓库名称",
              prop: "transferInName",
              type: "input",
              width:160,
              overHidden: true,
              sortable: true,
            },
            {
              label: "调拨日期",
              prop: "transferDate",
              type: "input",
              width:120,
              overHidden: true,
              sortable: true,
            },
            {
              label: "生成出仓单号",
              prop: "stockOutNo",
              type: "input",
              width:160,
              overHidden: true,
              sortable: true,
            },
            {
              label: "生成入仓单号",
              prop: "stockInNo",
              type: "input",
              width:160,
              overHidden: true,
              sortable: true,
            },
            {
              label: "备注",
              prop: "remark",
              type: "input",
              width:120,
              overHidden: true,
              sortable: true,
            },
            {
              label: "创建人",
              prop: "createUserName",
              type: "input",
              width:120,
              overHidden: true,
              sortable: true,
            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "input",
              width:120,
              overHidden: true,
              sortable: true,
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.stock_transfer_add, false),
          viewBtn: this.validData(this.permission.stock_transfer_view, false),
          delBtn: this.validData(this.permission.stock_transfer_delete, false),
          editBtn: this.validData(this.permission.stock_transfer_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("删除调拨单会同步删除关联出仓入仓单，确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleAdd(){
        userCompany().then(res => {
          if (res.data.success) {
            this.dept = res.data.data;
            if (!this.dept) {
              this.$message({
                type: "error",
                message: "用户无组织机构信息，请联系安伟工作人员！"
              });
            } else {
              let title="调拨单信息";
              let type='add';
              this.$refs.stockTransferForm.show({
                title,
                type
              })
            }
          }
        })
      },
      handleView(row) {
        let title="查看调拨单信息";
        let type='view';
        const id = row.id;
        this.$refs.stockTransferForm.show({
          id,
          title,
          type
        })
      },
      handleEdit(row) {
        let title="调拨单信息";
        let type='edit';
        const id = row.id;
        this.$refs.stockTransferForm.show({
          id,
          title,
          type
        })
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
